using System;
using System.Reflection;

namespace Com.ContentRendering.Api.DataEngine.Handler.Child
{
    /// <summary/>
    internal class CachedPropertyChildHandler : AbstractChildHandler
    {
        /// <summary/>
        internal CachedPropertyChildHandler(
            ReflectionCache reflectionCache,
            object data,
            string name)
            : base(
            reflectionCache,
            data,
            name) {}

        /// <summary/>
        internal override object Invoke()
        {
            Type type = base._data.GetType();

            // has it NOT been cached
            if (!base._reflectionCache.IsPropertyValid(type, base._name))
            {
                return base._successor.Invoke();
            }

            // fetch it
            PropertyInfo propertyInfo = base._reflectionCache.FetchProperty(type, base._name);

            // invoke from the cache
            return propertyInfo.GetValue(
                this._data,
                new object[] { });
        }
    }
}
